package com.zhou.framework.config;

import com.zhou.framework.util.SettingUtil;
import com.zhou.framework.aop.enums.LogLevel;
import com.zhou.framework.aop.enums.LogLevelEnum;
import com.zhou.framework.util.EnumClassUtil;
import lombok.Data;
import org.apache.commons.collections.MapUtils;

import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author lang.zhou
 * @since 2023/2/19
 */
@Data
public class SystemConfig {
    private static final Integer LOCK = 0;
    private CopyOnWriteArraySet<Integer> logLevel = new CopyOnWriteArraySet<>();

    private boolean crossOrigin;
    private boolean xFrameOptions;
    private boolean refererCheck;
    private boolean location;
    private String root;

    private SystemConfig(){
        Map<String, Object> system = SettingUtil.getSettingMap("system");
        this.setLogLevel(system);

        crossOrigin = MapUtils.getBoolean(system,"crossOrigin",true);
        xFrameOptions = MapUtils.getBoolean(system,"X-Frame-Options",true);
        refererCheck = MapUtils.getBoolean(system,"refererCheck",true);
        location = MapUtils.getBoolean(system,"location",false);
        root = MapUtils.getString(system,"root","");
    }

    private void setLogLevel(Map<String, Object> system){
        String[] arr = MapUtils.getString(system,"logLevel",String.valueOf(LogLevel.QUERY.value))
                .replace("，","").split(",");
        for (String s : arr) {
            LogLevelEnum le = EnumClassUtil.get(LogLevelEnum.class, Integer.parseInt(s.trim()));
            if(le != null){
                logLevel.add(le.getEnumValue());
            }
        }
    }

    private static SystemConfig config = new SystemConfig();

    public static SystemConfig getInstance(){
        return config;
    }

    public static void refresh(){
        synchronized (LOCK){
            config = new SystemConfig();
        }
    }


}
